let audio = wx.createInnerAudioContext({
        useWebAudioImplement: true
    }),
    lyric = '',
    address = '',
    wordName = {}
const db = wx.cloud.database()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        is_play: false,
        word: {}
    },

    getback: function () {
        wx.navigateBack()
    },

    //播放
    playmusic(address) {
        const {
            is_play
        } = this.data
        // this.music()
        if (is_play) {
            audio.pause();
            this.setData({
                is_play: false
            })
            console.log("暂停播放")
        } else if (!is_play) {
            audio.src = address
            audio.play();
            this.setData({
                is_play: true
            })
            console.log("继续播放")
        }
    },

    async music() {
        if (wordName.address) {
            this.playmusic(wordName.address)
        } else {
            let {
                result
            } = await wx.cloud.callFunction({
                name: 'queryWord',
                data: {
                    type: 'statusWord',
                    content: wordName.content
                }
            })
            console.log(result)
            if (result[0].address) { // 判断单词状态
                wx.showToast({
                    title: '该单词音频正在审核中',
                    icon: "none",
                    duration: 1500
                })
            } else {
                wx.showModal({ // TODO
                    cancelColor: 'cancelColor',
                    title: '提示',
                    content: '未查询到此单词音频',
                    confirmText: '去加音频',
                    cancelText: '取消',
                    success(res) {
                        if (res.confirm) {
                            wx.navigateTo({
                                url: '/pages/audio/upWordAudio/upWordAudio?id=' + wordName._id + '&content=' + wordName.content + '&type=' + wordName.type + '&uptype=' + "firstUp"
                            })
                            console.log(wordName.type)
                        }
                    }
                })
            }
        }
        console.log(this.data.is_play)
    },

    async onLoad(options) {
        console.log('op', options.content, options.id)
        wx.showLoading({
          mask:true
        })
        let {
            result
        } = await wx.cloud.callFunction({
            name: 'queryWord',
            data: {
                // id: options.id,
                content: options.content
            }
        })
        console.log('res', result)
        wordName = result[0]
        // return
        this.setData({
            word: wordName,
        })
        // lyric = wordName.lyric // TODO 歌词数据以获取
        // address = wordName.address
        // console.log(lyric, address)
        wx.hideLoading()
    },

    // 单词发音
    read: function () {
        this.playAudioByWord(wordName.content)
    },

    /**
     * 停止音频文件
     */
    stopAudio() {
        audio.stop()
    },

    /**
     * 根据单词，设置音频地址并播放。同时监听音频停止事件
     */
    playAudioByWord(word) {
        audio.src = `https://dict.youdao.com/dictvoice?audio=${word}&type=2`
        audio.play()
        audio.onStop(res => {
            // 清空音频链接
            audio.src = ''
        })
    },

    onUnload() {
        this.stopAudio() // 音频停止播放
    }
})